package com.totoro.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Service;
import com.totoro.domain.Resource;
import com.totoro.domain.RoleResource;

@Service("mySecurityMetadataSource")
public class MyInvocationSecurityMetadataSourceService implements
		FilterInvocationSecurityMetadataSource {

	private ResourceService resourceService;

	private RoleResourceService roleResourceService;

	// resourceMap及为key-url，value-Collection<ConfigAttribute>,资源权限对应Map
	private static Map<String, Collection<ConfigAttribute>> resourceMap = null;

	public MyInvocationSecurityMetadataSourceService() {
		System.out.println("resourceDao = " + resourceService);
	}

	@Autowired
	public MyInvocationSecurityMetadataSourceService(
			ResourceService resourceService,
			RoleResourceService roleResourceService) {
		this.resourceService = resourceService;
		this.roleResourceService = roleResourceService;
		System.out.println("加载MyInvocationSecurityMetadataSourceService..."
				+ resourceService);
		loadResourceDefine();
	}

	// 加载所有资源与权限的关系
	public void loadResourceDefine() {
		if (resourceMap == null) {
			resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
			// 加载所有的资源列表
			List<Resource> resources = resourceService.list();
			// 加载资源对应的权限
			for (Resource resource : resources) {
				// 创建权限集合
				Collection<ConfigAttribute> auths = new HashSet<ConfigAttribute>(
						0);
				// 将对应的资源遍历出来
				for (Iterator<?> ite = roleResourceService.list("resource",
						resource).iterator(); ite.hasNext();) {
					RoleResource roleResource = (RoleResource) ite.next();
					System.out.println("resource = " + resource.getNo()
							+ " 权限是：" + roleResource.getRole().getName());
					ConfigAttribute ca = new SecurityConfig(roleResource.getRole()
							.getName());
					auths.add(ca);
				}
				System.out.println("资源：" + resource.getNo() + "  权限=" + auths);
				resourceMap.put(resource.getNo(), auths);
			}
		}
	}

	// 加载所有资源与权限的关系
	@Override
	public Collection<ConfigAttribute> getAttributes(Object object)
			throws IllegalArgumentException {
		// object是一个URL，被用户请求的url
		String requestUrl = ((FilterInvocation) object).getRequestUrl();
		System.out.println("requestUrl is " + requestUrl);

		int firstQuestionMarkIndex = requestUrl.indexOf("?");

		if (firstQuestionMarkIndex != -1) {
			requestUrl = requestUrl.substring(0, firstQuestionMarkIndex);
		}

		if (resourceMap == null) {
			loadResourceDefine();
		}
		//
		Iterator<String> ite = resourceMap.keySet().iterator();

		while (ite.hasNext()) {
			String resURL = ite.next();
			System.out.println("资源列表中的资源：" + resURL);
			if (resURL.equals(requestUrl)) {
				return resourceMap.get(resURL);
			}
		}
		return null;
	}

	@Override
	public boolean supports(Class<?> arg0) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		// TODO Auto-generated method stub
		return null;
	}

}
